const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');

const Tag = sequelize.define('Tag', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  name: {
    type: DataTypes.STRING(50),
    allowNull: false,
    unique: true
  },
  color: {
    type: DataTypes.STRING(20),
    defaultValue: '#409EFF',
    allowNull: false
  },
  description: {
    type: DataTypes.STRING(200),
    allowNull: true
  }
}, {
  tableName: 'tags'
});

// 批量创建或获取标签
Tag.findOrCreateTags = async function(tagNames) {
  const tags = await Promise.all(
    tagNames.map(name => this.findOrCreate({
      where: { name },
      defaults: { name }
    }))
  );
  return tags.map(tag => tag[0]);
};

module.exports = Tag;